package controller

import (
	"common/helper"
	"encoding/json"
	"github.com/valyala/fasthttp"
	"member/contrib/validator"
	"member/model"
)

type ThirdController struct{}

var onlinePay = map[string]bool{
	"666777888": true, //	EPAY
	"666777999": true, //	EPAYU
}

type depositParam struct {
	Fid    string `json:"fid" cbor:"fid"`
	Amount string `json:"amount" cbor:"amount"`
	Flag   string `json:"flag" cbor:"flag"` //1 参与 2 不参与
}

type withdrawParam struct {
	Amount      string `json:"amount" cbor:"amount"`             //金额
	BankId      string `json:"bank_id" cbor:"bank_id"`           //银行id
	PayPassword string `json:"pay_password" cbor:"pay_password"` //支付密码
	Fid         string `json:"fid" cbor:"fid"`
	Flag        string `json:"flag"`
}

func (that ThirdController) Deposit(ctx *fasthttp.RequestCtx) {

	prefix := string(ctx.Request.Header.Peek("X-Prefix"))
	param := depositParam{}
	data := ctx.PostBody()
	err := json.Unmarshal(data, &param)
	if err != nil {
		helper.Print(ctx, false, helper.ParamErr)
		return
	}
	if !helper.CtypeDigit(param.Amount) {
		helper.Print(ctx, false, helper.AmountErr)
		return
	}

	res, err := model.Recharge(ctx, prefix, param.Fid, param.Amount, param.Flag)
	if err != nil {
		helper.Print(ctx, false, err.Error())
		return
	}
	helper.Print(ctx, true, res)
	//return
	//helper.Print(ctx, false, helper.PayServerErr)
}

func (that ThirdController) DepositChannel(ctx *fasthttp.RequestCtx) {

	data, err := model.ChannelList(ctx, 1)
	if err != nil {
		helper.Print(ctx, false, err)
		return
	}
	helper.Print(ctx, true, data)
}

func (that ThirdController) WithdrawConfig(ctx *fasthttp.RequestCtx) {

	data, err := model.WithdrawConfig(ctx)
	if err != nil {
		helper.Print(ctx, false, err)
		return
	}
	helper.Print(ctx, true, data)
}

func (that ThirdController) CallbackEpay(ctx *fasthttp.RequestCtx) {

	model.DepositCallBack(ctx)
}

func (that ThirdController) CallbackJeepay(ctx *fasthttp.RequestCtx) {
	model.DepositJeepayCallBack(ctx)
}

func (that ThirdController) CallbackCepay(ctx *fasthttp.RequestCtx) {
	model.DepositCepayCallBack(ctx)
}
func (that ThirdController) CallbackU2CPay(ctx *fasthttp.RequestCtx) {
	model.DepositPayU2CCallBack(ctx)
}

func (that ThirdController) CallbackPay4z(ctx *fasthttp.RequestCtx) {
	model.DepositPay4zCallBack(ctx)
}

func (that ThirdController) CallbackBetcatpay(ctx *fasthttp.RequestCtx) {
	model.DepositBetcatpayCallBack(ctx)
}

//func (that ThirdController) CallbackEpayUsdt(ctx *fasthttp.RequestCtx) {
//
//	model.DepositCallBack(ctx, "666777999")
//}

func (that ThirdController) Withdraw(ctx *fasthttp.RequestCtx) {

	//prefix := string(ctx.Request.Header.Peek("X-Prefix"))

	param := withdrawParam{}
	data := ctx.PostBody()
	err := json.Unmarshal(data, &param)
	if err != nil {
		helper.Print(ctx, false, helper.ParamErr)
		return
	}
	if !validator.CheckMoney(param.Amount) {
		helper.Print(ctx, false, helper.AmountErr)
		return
	}
	if param.BankId == "" {
		helper.Print(ctx, false, helper.BankcardIDErr)
		return
	}

	res, err := model.WithdrawApply(ctx, param.Amount, param.BankId, param.PayPassword, param.Fid, param.Flag)
	if err != nil {
		helper.Print(ctx, false, err.Error())
		return
	}
	helper.Print(ctx, true, res)
	return
}

func (that ThirdController) DepositList(ctx *fasthttp.RequestCtx) {
	flag := ctx.QueryArgs().GetUintOrZero("flag") //查询类型 0 所有 ,1 昨天, 2 今天,3 7天, 4 15天,5 30天
	data, err := model.DepositList(ctx, flag)
	if err != nil {
		helper.Print(ctx, false, err.Error())
		return
	}
	helper.Print(ctx, true, data)
}

func (that ThirdController) WithdrawList(ctx *fasthttp.RequestCtx) {
	flag := ctx.QueryArgs().GetUintOrZero("flag") //查询类型 0 所有 ,1 昨天, 2 今天,3 7天, 4 15天,5 30天
	data, err := model.WithdrawList(ctx, flag)
	if err != nil {
		helper.Print(ctx, false, err.Error())
		return
	}
	helper.Print(ctx, true, data)
}

func (that ThirdController) DepositDiscountList(ctx *fasthttp.RequestCtx) {
	data, err := model.DepositDiscountList(ctx)
	if err != nil {
		helper.Print(ctx, false, err.Error())
		return
	}
	helper.Print(ctx, true, data)
}

func (that ThirdController) DepositDiscountApply(ctx *fasthttp.RequestCtx) {
	param := model.UserDepositDiscountList{}
	data := ctx.PostBody()
	err := json.Unmarshal(data, &param)
	if err != nil {
		helper.Print(ctx, false, helper.ParamErr)
		return
	}

	err = model.DepositDiscountApply(ctx, param.Id, param.Discount)
	if err != nil {
		helper.Print(ctx, false, err.Error())
		return
	}
	helper.Print(ctx, true, helper.Success)
}
